package org.adl.registry.website;

import com.sun.jndi.toolkit.url.Uri;
import java.net.MalformedURLException;
import java.util.ArrayList;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import org.adl.registry.website.mail.EmailMessage;

/**
 *
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class RepositoryRegistrationRequest
{
    private String mRepositoryName;
    private String mRepositoryDescription;
    private Uri mRepositoryLocation;
    
    private String mSponsorRepresentative;
    private String mSponsorOrganization;
    private InternetAddress mSponsorRepresentativeEmail;
    private String mSponsorRepresentativePhone;
    private String mSponsorRepresentativeAddress1;
    private String mSponsorRepresentativeAddress2;
    private String mSponsorRepresentativeCity;
    private String mSponsorRepresentativeState;
    private String mSponsorRepresentativeZip;
    
    private String mProponentOrganization;
    private String mProponentRepresentativeName;
    private String mProponentRepresentativePhone;
    private InternetAddress mProponentRepresentativeEmail;
    
    private String mNamingAuthorities;
    private String mAccessInfo;
    private String mSecurityInfo;
    private String mComments;
    
    /**
     * Creates a new instance of RepositoryRegistration
     */
    public RepositoryRegistrationRequest()
    {
        
    }
    
    public boolean isValid()
    {
        boolean valid = true;
        
        if ((this.mRepositoryName == null) || (this.mRepositoryName.length() == 0))
        {
            valid = false;
        }
        if ((this.mRepositoryDescription == null) || (this.mRepositoryDescription.length() == 0))
        {
            valid = false;
        }
        if (this.mRepositoryLocation == null)
        {
            valid = false;
        }
        if ((this.mSponsorRepresentative == null) || (this.mSponsorRepresentative.length() == 0))
        {
            valid = false;
        }
        if ((this.mSponsorOrganization == null) || (this.mSponsorOrganization.length() == 0))
        {
            valid = false;
        }
        if (this.mSponsorRepresentativeEmail == null)
        {
            valid = false;
        }
        if ((this.mSponsorRepresentativePhone == null) || (this.mSponsorRepresentativePhone.length() == 0))
        {
            valid = false;
        }
        if ((this.mSponsorRepresentativeAddress1 == null) || (this.mSponsorRepresentativeAddress1.length() == 0))
        {
            valid = false;
        }
        if ((this.mSponsorRepresentativeCity == null) || (this.mSponsorRepresentativeCity.length() == 0))
        {
            valid = false;
        }
        if ((this.mSponsorRepresentativeState == null) || (this.mSponsorRepresentativeState.length() == 0))
        {
            valid = false;
        }
        if ((this.mSponsorRepresentativeZip == null) || (this.mSponsorRepresentativeZip.length() == 0))
        {
            valid = false;
        }
        
        if ((this.mProponentOrganization == null) || (this.mProponentOrganization.length() == 0))
        {
            valid = false;
        }
        if ((this.mProponentRepresentativeName == null) || (this.mProponentRepresentativeName.length() == 0))
        {
            valid = false;
        }
        if ((this.mProponentRepresentativePhone == null) || (this.mProponentRepresentativePhone.length() == 0))
        {
            valid = false;
        }
        if (this.mProponentRepresentativeEmail == null)
        {
            valid = false;
        }
        
        return valid;
    }
    
    public void sendNotifications(HttpServletRequest iRequest)
    throws MessagingException
    {
        EmailMessage message = new EmailMessage();
        
        try
        {
            message.setSender("adlrhelpdesk@adlnet.gov");
            message.addRecipient("jacob.marks.ctr@adlnet.gov");
            message.addRecipient("adlrhelpdesk@adlnet.gov");
            message.addRecipient("gredding@ida.org");
        }
        catch (AddressException ex)
        {
            // this should never occur.
        }
        
        message.setSubject("ADL Registry Repository Registration Request.");
        
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Repository Information\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Title: " + this.mRepositoryName + "\r");
        message.addLineToBody("Description: \r" + this.mRepositoryDescription + "\r");
        message.addLineToBody("Location: " + this.mRepositoryLocation + "\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Sponsor/Proponent Information\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Sponsor: " + this.mSponsorOrganization + "\r");
        message.addLineToBody("Name: " + this.mSponsorRepresentative + "\r");
        message.addLineToBody("E-Mail: " + this.mSponsorRepresentativeEmail + "\r");
        message.addLineToBody("Phone: " + this.mSponsorRepresentativePhone + "\r");
        message.addLineToBody("Address:\r" + this.mSponsorRepresentativeAddress1 + "\r" + this.mSponsorRepresentativeAddress2 + "\r");
        message.addLineToBody("City: " + this.mSponsorRepresentativeCity + "\r");
        message.addLineToBody("State: " + this.mSponsorRepresentativeState + "\r");
        message.addLineToBody("Zip: " + this.mSponsorRepresentativeZip + "\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Component Proponent Information\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Proponent Office: " + this.mProponentOrganization + "\r");
        message.addLineToBody("Proponent Rep: " + this.mProponentRepresentativeName + "\r");
        message.addLineToBody("Proponent Email: " + this.mProponentRepresentativeEmail + "\r");
        message.addLineToBody("Proponent Phone: " + this.mProponentRepresentativePhone + "\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Additional Information\r");
        message.addLineToBody("**************************************************\r");
        message.addLineToBody("Naming Autorities:\r" + this.mNamingAuthorities + "\r");
        message.addLineToBody("Access Information:\r" + this.mAccessInfo + "\r");
        message.addLineToBody("Security Information:\r" + this.mSecurityInfo + "\r");
        message.addLineToBody("Comments:\r" + this.mComments);
        
        message.send(iRequest);
    }
    
    public String getRepositoryName()
    {
        return this.mRepositoryName;
    }
    
    public void setRepositoryName(String iRepositoryName)
    throws FormFieldValidationException
    {
        if ((iRepositoryName == null) || (iRepositoryName.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository name is required.");
        }
        
        this.mRepositoryName = iRepositoryName.trim();
    }
    
    public String getRepositoryDescription()
    {
        return this.mRepositoryDescription;
    }
    
    public void setRepositoryDescription(String iRepositoryDescription)
    throws FormFieldValidationException
    {
        if ((iRepositoryDescription == null) || (iRepositoryDescription.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository description is required.");
        }
        
        this.mRepositoryDescription = iRepositoryDescription;
    }
    
    public Uri getRepositoryLocation()
    {
        return this.mRepositoryLocation;
    }
    
    public void setRepositoryLocation(String iRepositoryLocation)
    throws FormFieldValidationException
    {
        if ((iRepositoryLocation == null) || (iRepositoryLocation.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository description is required.");
        }
        
        Uri uri;
        
        try
        {
            uri = new Uri(iRepositoryLocation);
        }
        catch (MalformedURLException ex)
        {
            throw new FormFieldValidationException(ex.getMessage());
        }
        
        this.mRepositoryLocation = uri;
    }
    
    public String getSponsorRepresentative()
    {
        return this.mSponsorRepresentative;
    }
    
    public void setSponsorRepresentative(String iSponsorRepresentative)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentative == null) || (iSponsorRepresentative.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor representative is required.");
        }
        
        this.mSponsorRepresentative = iSponsorRepresentative;
    }
    
    public String getSponsorOrganization()
    {
        return this.mSponsorOrganization;
    }
    
    public void setSponsorOrganization(String iSponsorOrganization)
    throws FormFieldValidationException
    {
        if ((iSponsorOrganization == null) || (iSponsorOrganization.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor organization is required.");
        }
        
        this.mSponsorOrganization = iSponsorOrganization;
    }
    
    public InternetAddress getSponsorRepresentativeEmail()
    {
        return this.mSponsorRepresentativeEmail;
    }
    
    public void setSponsorRepresentativeEmail(String iSponsorRepresentativeEmail)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentativeEmail == null) || (iSponsorRepresentativeEmail.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor representative email is required.");
        }
        
        InternetAddress address;
        
        try
        {
            address = new InternetAddress(iSponsorRepresentativeEmail, true);
        }
        catch (AddressException ex)
        {
            throw new FormFieldValidationException(ex.getMessage());
        }
        
        this.mSponsorRepresentativeEmail = address;
    }
    
    public String getSponsorRepresentativePhone()
    {
        return this.mSponsorRepresentativePhone;
    }
    
    public void setSponsorRepresentativePhone(String iSponsorRepresentativePhone)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentativePhone == null) || (iSponsorRepresentativePhone.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor  representative phone is required.");
        }
        
        this.mSponsorRepresentativePhone = iSponsorRepresentativePhone;
    }
    
    public String getSponsorRepresentativeAddress1()
    {
        return this.mSponsorRepresentativeAddress1;
    }
    
    public void setSponsorRepresentativeAddress1(String iSponsorRepresentativeAddress1)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentativeAddress1 == null) || (iSponsorRepresentativeAddress1.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor representative address 1 is required.");
        }
        
        this.mSponsorRepresentativeAddress1 = iSponsorRepresentativeAddress1;
    }
    
    public String getSponsorRepresentativeAddress2()
    {
        return mSponsorRepresentativeAddress2;
    }
    
    public void setSponsorRepresentativeAddress2(String iSponsorRepresentativeAddress2)
    {
        // This value is not required.
        this.mSponsorRepresentativeAddress2 = iSponsorRepresentativeAddress2;
    }
    
    public String getSponsorRepresentativeCity()
    {
        return mSponsorRepresentativeCity;
    }
    
    public void setSponsorRepresentativeCity(String iSponsorRepresentativeCity)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentativeCity == null) || (iSponsorRepresentativeCity.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor representative city is required.");
        }
        
        this.mSponsorRepresentativeCity = iSponsorRepresentativeCity;
    }
    
    public String getSponsorRepresentativeState()
    {
        return mSponsorRepresentativeState;
    }
    
    public void setSponsorRepresentativeState(String iSponsorRepresentativeState)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentativeState == null) || (iSponsorRepresentativeState.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor representative state is required.");
        }
        
        this.mSponsorRepresentativeState = iSponsorRepresentativeState;
    }
    
    public String getSponsorRepresentativeZip()
    {
        return mSponsorRepresentativeZip;
    }
    
    public void setSponsorRepresentativeZip(String iSponsorRepresentativeZip)
    throws FormFieldValidationException
    {
        if ((iSponsorRepresentativeZip == null) || (iSponsorRepresentativeZip.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository sponsor representative zip is required.");
        }
        
        this.mSponsorRepresentativeZip = iSponsorRepresentativeZip;
    }
    
    public String getProponentOrganization()
    {
        return mProponentOrganization;
    }
    
    public void setProponentOrganization(String iProponentOrganization)
    throws FormFieldValidationException
    {
        if ((iProponentOrganization == null) || (iProponentOrganization.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository proponent organization is required.");
        }
        
        this.mProponentOrganization = iProponentOrganization;
    }
    
    public String getProponentRepresentativeName()
    {
        return mProponentRepresentativeName;
    }
    
    public void setProponentRepresentativeName(String iProponentRepresentativeName)
    throws FormFieldValidationException
    {
        if ((iProponentRepresentativeName == null) || (iProponentRepresentativeName.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository proponent representative is required.");
        }
        
        this.mProponentRepresentativeName = iProponentRepresentativeName;
    }
    
    public String getProponentRepresentativePhone()
    {
        return mProponentRepresentativePhone;
    }
    
    public void setProponentRepresentativePhone(String iProponentRepresentativePhone)
    throws FormFieldValidationException
    {
        if ((iProponentRepresentativePhone == null) || (iProponentRepresentativePhone.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository proponent representative phone is required.");
        }
        
        this.mProponentRepresentativePhone = iProponentRepresentativePhone;
    }
    
    public InternetAddress getProponentRepresentativeEmail()
    {
        return mProponentRepresentativeEmail;
    }
    
    public void setProponentRepresentativeEmail(String iProponentRepresentativeEmail)
    throws FormFieldValidationException
    {
        if ((iProponentRepresentativeEmail == null) || (iProponentRepresentativeEmail.trim().length() == 0))
        {
            throw new FormFieldValidationException("A repository proponent representative email is required.");
        }
        
        InternetAddress address;
        
        try
        {
            address = new InternetAddress(iProponentRepresentativeEmail, true);
        }
        catch (AddressException ex)
        {
            throw new FormFieldValidationException(ex.getMessage());
        }
        
        this.mProponentRepresentativeEmail = address;
    }
    
    public String getNamingAuthorities()
    {
        return mNamingAuthorities;
    }
    
    public void setNamingAuthorities(String mNamingAuthorities)
    {
        // This value is not required.
        this.mNamingAuthorities = mNamingAuthorities;
    }
    
    public String getAccessInfo()
    {
        return mAccessInfo;
    }
    
    public void setAccessInfo(String mAccessInfo)
    {
        // This value is not required.
        this.mAccessInfo = mAccessInfo;
    }
    
    public String getSecurityInfo()
    {
        return mSecurityInfo;
    }
    
    public void setSecurityInfo(String mSecurityInfo)
    {
        // This value is not required.
        this.mSecurityInfo = mSecurityInfo;
    }
    
    public String getComments()
    {
        return mComments;
    }
    
    public void setComments(String mComments)
    {
        // This value is not required.
        this.mComments = mComments;
    }
}
